﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Lucene.Net.Index;

public partial class productmanage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["USERNAME"] == null)
        {
            Response.Redirect("error_log.aspx", true);
        }
        if (Session["IDENTITY"].Equals("employee") == true)
        {
            label_tag.Visible = false;
            textbox1.Visible = false;
            button1.Visible = false;
        }
        string name = Session["USERNAME"].ToString();
        SqlConnection sc = new SqlConnection(ConfigurationManager.ConnectionStrings["wbkpt"].ConnectionString.ToString());
        sc.Open();
        SqlCommand com_get = new SqlCommand();
        com_get.Connection = sc;
        com_get.CommandText = "select PRODUCT_ID,PRODUCT_NAME,CREDITS_RATE,PRODUCT_SORT,PRODUCT_INTRO,PRODUCT_IMAGE,SPECIAL from INFO_PRODUCT where ENTER_ID=@eid1";
        com_get.Parameters.AddWithValue("@eid1",Session["ENTERID"].ToString());
        SqlDataReader reader = com_get.ExecuteReader();
        GridView1.DataSource = reader;
        //ImageField newi = new ImageField();
        
        //GridView1.Columns.Add(newi);
        GridView1.DataBind();
        
        sc.Close();

    }
     protected void delete(object sender, GridViewDeleteEventArgs e)
    {
        if (Session["IDENTITY"].Equals("employee") == true)
        {
            Response.Redirect("error_auth.aspx", true);
        }
        String id = GridView1.Rows[e.RowIndex].Cells[0].Text;
           SqlConnection sc1 = new SqlConnection(ConfigurationManager.ConnectionStrings["wbkpt"].ConnectionString.ToString());
              sc1.Open();
                SqlCommand com_deletesale = new SqlCommand();

                com_deletesale.Connection = sc1;
                com_deletesale.CommandText = "delete from PRODUCT_SALE where PRODUCT_ID=@ida";
                com_deletesale.Parameters.AddWithValue("@ida",id);
                while (Application["sqllock"] == "lock")
                {//检测是否已经锁住               
                    System.Threading.Thread.Sleep(2000);
                }
                Application["sqllock"] = "lock";//上锁  
                try
                {
                    com_deletesale.ExecuteNonQuery();
                }
                finally
                {
                    Application["sqllock"] = "unlock";//解锁 
                }
                SqlCommand com_delete=new SqlCommand();
                com_delete.Connection=sc1;
                com_delete.CommandText="delete from INFO_PRODUCT where PRODUCT_ID=@idb";
                com_delete.Parameters.AddWithValue("@idb",id);
                while (Application["sqllock"] == "lock")
                {//检测是否已经锁住               
                    System.Threading.Thread.Sleep(2000);
                }
                Application["sqllock"] = "lock";//上锁 
                 
                if(com_delete.ExecuteNonQuery()==1)
                {
                    Application["sqllock"] = "unlock";//解锁
                  
                    int t_count = 0;
                    while (System.IO.File.Exists(Server.MapPath("~/product_image/" + id + "_image" + t_count + ".jpg")))
                    {
                        System.IO.File.Delete(Server.MapPath("~/product_image/" + id + "_image" + t_count + ".jpg"));
                        t_count++;
                    }

                    string enterid = Session["ENTERID"].ToString();
                    IndexReader reader = IndexReader.Open(Server.MapPath("index/index_" + enterid));
                    Term term = new Term("pid", id);
                    reader.DeleteDocuments(term);
                   
                    reader.Close();
                    sc1.Close();
                    Response.Write("<script language=\"javascript\" type=\"text/javascript\">alert('删除产品信息成功！');</script>");
                    return;
                }
                else
                {
                    Application["sqllock"] = "unlock";//解锁 
                    sc1.Close();
                    Page.RegisterStartupScript("ss", "<script>alert('数据库异常，请重试！')</script>");
                    return;
                }
            
            }
     protected void button1_Click(object sender, EventArgs e)
     {
         SqlConnection sc2 = new SqlConnection(ConfigurationManager.ConnectionStrings["wbkpt"].ConnectionString.ToString());
         sc2.Open();
         SqlCommand check_id=new SqlCommand();
         check_id.Connection = sc2;
         check_id.CommandText="select PRODUCT_NAME from INFO_PRODUCT where PRODUCT_ID=@ida and ENTER_ID=@eid2";
         check_id.Parameters.AddWithValue("@ida",textbox1.Text);
         check_id.Parameters.AddWithValue("@eid2",Session["ENTERID"].ToString());
         if(check_id.ExecuteScalar()==null)
         {
              sc2.Close();
              Page.RegisterStartupScript("ss", "<script>alert('您输入的ID有误或不属于您的平台！')</script>");
             return;
         }
         else//输入的ID存在且属于登录用户，执行更新操作
         {
             try  
             {
             SqlCommand com = new SqlCommand("update INFO_PRODUCT set SPECIAL=@spec where PRODUCT_ID=@id",sc2);
             com.Parameters.AddWithValue("@spec","TRUE");
             com.Parameters.AddWithValue("@id",textbox1.Text);
             while (Application["sqllock"] == "lock")
             {//检测是否已经锁住               
                 System.Threading.Thread.Sleep(2000);
             }
             Application["sqllock"] = "lock";//上锁 
           
             if (com.ExecuteNonQuery() == 1)
             { 
                 Application["sqllock"] = "unlock";//解锁 
                 sc2.Close();
                 Page.RegisterStartupScript("ss", "<script>alert('产品ID为" + textbox1.Text + "的产品发布为精选商品成功！')</script>");
                 return;
             }
             else
             {
                 Application["sqllock"] = "unlock";//解锁 
                 sc2.Close();
                 Page.RegisterStartupScript("ss", "<script>alert('数据库异常，请重试！')</script>");
                 return;
             }
             }
             catch(Exception ee)
             {
                 sc2.Close();
                 Page.RegisterStartupScript("ss", "<script>alert('操作终止，错误代码："+ee.StackTrace+"')</script>");
                 return;
             }
          }
         
     }
}